#!/bin/bash               
                                                                                               
function checks {
        if [ -z "$1" ]                                                                                                                                                                         
        then
                echo "Missing token file"                                                                                                                                                      
                exit 1
        fi                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                               
        if [ -z "$2" ]                                                                                                                                                                         
        then              
                echo "Missing gitlab url"               
                exit 1      
        fi
                                                                                               
        if [ ! -f "$1" ]                                                                                                                                                                                                                                                                                                                                                                      
        then      
                echo "Token file not found"                                                                                                                                                                                                                                                                                                                                                   
                exit 1    
        fi                                                                                                                                                                                                                                                                                                                                                                                    
}                                 

function get_subgroups {
        curl --silent --header "Authorization: Bearer ${TOKEN}" ${GITLAB_URL}/api/v4/groups/${1}/subgroups?per_page=100 | jq -r '.[].id'
}

function clone_project {
        PROJECT_INFO=$(curl --silent --header "Authorization: Bearer ${TOKEN}" ${GITLAB_URL}/api/v4/projects/${1} | jq .)
        echo "INFO: Check project $(echo ${PROJECT_INFO} | jq -r '.path_with_namespace')"
        if [ ! -d "$(echo ${PROJECT_INFO} | jq -r '.path_with_namespace')" ]
        then
                echo "INFO: Clone project $(echo ${PROJECT_INFO} | jq -r '.path_with_namespace')"
                mkdir -p $(echo ${PROJECT_INFO} | jq -r '.path_with_namespace')
                git clone $(echo ${PROJECT_INFO} | jq -r '. | "\(.ssh_url_to_repo) \(.path_with_namespace)"')
        fi
}

checks ${1} ${2}

# Vars
declare -r GITLAB_URL=${2}
declare -r TOKEN=$(cat ${1})

for PAGE in $(seq 1 $(curl -X HEAD -w '%{header_json}' --header "Authorization: Bearer ${TOKEN}" ${GITLAB_URL}/api/v4/groups?per_page=100 2>/dev/null| jq -r '."x-total-pages"[0]'))
do
        for GROUP in $(curl --silent --header "Authorization: Bearer ${TOKEN}" "${GITLAB_URL}/api/v4/groups?per_page=100&page=${PAGE}" | jq -r '.[].id')
        do
                for P_PAGE in $(seq 1 $(curl -X HEAD -w '%{header_json}' --header "Authorization: Bearer ${TOKEN}"  ${GITLAB_URL}/api/v4/groups/${GROUP}/projects?per_page=100 2>/dev/null| jq -r '."x-total-pages"[0]'))
                do
                        for PROJECT in $(curl --silent --header "Authorization: Bearer ${TOKEN}" "${GITLAB_URL}/api/v4/groups/${GROUP}/projects?per_page=100&page=${P_PAGE}" | jq -r '.[].id')
                        do
                                clone_project ${PROJECT}
                        done

                done
                for SG_PAGE in $(seq 1 $(curl -X HEAD -w '%{header_json}' --header "Authorization: Bearer ${TOKEN}"  ${GITLAB_URL}/api/v4/groups/${GROUP}/subgroups?per_page=100 2>/dev/null | jq -r '."x-total-pages"[0]'))
                do
                        for SUBGROUP in $(curl --silent --header "Authorization: Bearer ${TOKEN}" "${GITLAB_URL}/api/v4/groups/${GROUP}/subgroups?per_page=100&page=${SG_PAGE}" | jq -r '.[].id')
                        do
                                for P_SG_PAGE in $(seq 1 $(curl -X HEAD -w '%{header_json}' --header "Authorization: Bearer ${TOKEN}"  ${GITLAB_URL}/api/v4/groups/${SUBGROUP}/projects?per_page=100 2>/dev/null| jq -r '."x-total-pages"[0]'))
                                do
                                        for PROJECT in $(curl --silent --header "Authorization: Bearer ${TOKEN}" "${GITLAB_URL}/api/v4/groups/${SUBGROUP}/projects?per_page=100&page=${P_SG_PAGE}" | jq -r '.[].id')
                                        do
                                                clone_project ${PROJECT}
                                        done

                                done

                        done
                done
        done
done
echo "OK"
